#!/usr/bin/env python3
# coding:utf-8
import rospy
import signal
from contnav_srvs.srv import ContnavLogger, ContnavLoggerRequest, ContnavLoggerResponse

import sys
'''
python 测试节点
sukai
2022-11-21
rosrun contnav testLog.py
'''
logFileName="testlog.txt";
stop = False

# # #日志级别 日志内容
def saveLogFormat(logLevel,fileline,logcontent):
    # 创建请求数据
    request = ContnavLoggerRequest()
    request.request_type = "savelog" #保存日志
    request.logFileName = logFileName #日志文件名称
    request.className = __file__ #代码file名称 [注意： 确保每个服务节点只使用一个日志文件名,日志服务节点是根据文件名来创建对应的日志对象，请确保一个服务对应一个日志文件名称]
    request.classLine = fileline #代码行数据
    request.logLevel = logLevel    #日志类型,默认 debug
    request.logcontent = logcontent  #日志内容

    # 调用服务并且获得响应结果
    response = client.call(request)

    if isinstance(response, ContnavLoggerResponse):
        print (response.result)
        if(response.result!="ok"):
            print ("日志写入失败！")


# debug
def contnav_debug(fileline,logcontent, *arg):
    try:
        logcontent=logcontent%arg
    except:
        pass

    print (logcontent)
    saveLogFormat('debug',fileline,logcontent)

# info
def contnav_info(fileline,logcontent, *arg):
    try:
        logcontent=logcontent%arg
    except:
        pass

    print (logcontent)
    saveLogFormat('info',fileline,logcontent)

# warn
def contnav_warn(fileline,logcontent, *arg):
    try:
        logcontent=logcontent%arg
    except:
        pass

    print (logcontent)
    saveLogFormat('warn',fileline,logcontent)

# error
def contnav_error(fileline,logcontent, *arg):
    try:
        logcontent=logcontent%arg
    except:
        pass

    print (logcontent)
    saveLogFormat('error',fileline,logcontent)

# fatal
def contnav_fatal(fileline,logcontent, *arg):
    try:
        logcontent=logcontent%arg
    except:
        pass

    print (logcontent)
    saveLogFormat('fatal',fileline,logcontent)


# 监听ctrl c
def my_handler(signum, frame):
    global stop
    stop = True
    #关闭节点
    rospy.signal_shutdown("ctrl c!")
    if rospy.is_shutdown():
        print("close ros  test python")
    print("终止")

#  roslaunch contnav   contnavTest.launch
#  rosrun contnav testLog.py
if __name__ == '__main__':
    # 创建节点
    rospy.init_node("testLog_node")

    # 监听ctrl c
    signal.signal(signal.SIGINT, my_handler)    #读取Ctrl+c信号
    print("hello ros python")

    # 创建日志 Service Client
    serviceName = "/contnavLogger"
    client = rospy.ServiceProxy(serviceName, ContnavLogger)

    try:
        #读取launch中的参数
        logFileName=rospy.get_param('/testLog_node/logFileName')
    except:
        print ("未读取到launch中的参数！")

    print(logFileName)
    # 等待服务开启
    #rospy.wait_for_service(serviceName)
    # sys._getframe().f_lineno 当前行
    contnav_debug(sys._getframe().f_lineno,"1 sukai %d is greater than %d",1,2)
    contnav_info(sys._getframe().f_lineno,"2 sukai %d is greater than %d",1,2)
    contnav_warn(sys._getframe().f_lineno,"3 sukai %d is greater than %d",1,2)
    contnav_error(sys._getframe().f_lineno,"4 sukai %d is greater than %d",1,2)
    contnav_fatal(sys._getframe().f_lineno,"5 sukai %d is greater than %d",1,2)




    rospy.spin()
